package com.xuecheng.media.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XuChengException;
import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author luhao
 * @ClassName MediaProcessServiceImpl.java
 * @Description
 * @createTime 2023/7/26
 */
@Service
@Slf4j
public class MediaProcessServiceImpl extends ServiceImpl<MediaProcessMapper, MediaProcess> implements MediaProcessService {
    @Autowired
    private MediaFilesMapper mediaFilesMapper;
    @Autowired
    private MediaProcessHistoryMapper mediaProcessHistoryMapper;
    @Override
    public List<MediaProcess> getListByShardIndex(Integer shardingIndex, Integer shardingTotal, Integer total) {
        List<MediaProcess> mediaProcesses = this.baseMapper.selectListByShardIndex(shardingIndex, shardingTotal, total);
        return mediaProcesses;
    }

    /**
     * 开启一个任务
     *
     * @param id 任务id
     * @return 是否成功
     */
    @Override
    public boolean startTaskById(Long id) {
        int task = this.baseMapper.startTask(id);
        return task > 0 ? true : false;
    }

    /**
     * 保存处理后的信息
     *
     * @param taskId   任务id
     * @param status   状态
     * @param fileId   文件Id
     * @param url      文件url
     * @param errorMsg 错误信息
     * @return 是否出处理成功
     */
    @Override
    @Transactional
    public boolean saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg,String fileName) {
        // 根据任务id查询任务
        MediaProcess mediaProcess = this.baseMapper.selectById(taskId);
        if (ObjectUtil.isNull(mediaProcess)) {
            XuChengException.cast("文件处理任务不存在");
        }
        // 文件处理失败的
        if ("3".equals(status)) {
            mediaProcess.setStatus(status);
            mediaProcess.setErrormsg(errorMsg);
            mediaProcess.setFailCount(mediaProcess.getFailCount() + 1);
            // 更新媒资处理信息
            this.baseMapper.updateById(mediaProcess);
            log.info("文件:{},处理失败,失败原因:{}",mediaProcess.getId(),mediaProcess.getErrormsg());
            return true;
        }
        // 文件处理成功,查询文件信息
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
        if (ObjectUtil.isNull(mediaFiles)) {
            XuChengException.cast("文件信息不存在");
        }
        mediaFiles.setUrl(url);
        mediaFiles.setChangeDate(LocalDateTime.now());
        // 处理好的文件路径
        String filePath = StrUtil.replace(url, "/" + mediaFiles.getBucket(), "");
        mediaFiles.setFilePath(filePath);
        mediaFiles.setUrl(url);
        mediaFiles.setFilename(fileName);
        // 更新文件信息
        mediaFilesMapper.updateById(mediaFiles);
        // 设置文件处理状态为2
        mediaProcess.setStatus("2");
        mediaProcess.setUrl(url);
        mediaProcess.setFinishDate(LocalDateTime.now());
        mediaProcess.setFilename(fileName);
        mediaProcess.setFilePath(filePath);
        this.baseMapper.updateById(mediaProcess);
        // 拷贝文件处理信息到 media_process_history 表种
        MediaProcessHistory mediaProcessHistory = BeanUtil.copyProperties(mediaProcess, MediaProcessHistory.class, "id");
        mediaProcessHistoryMapper.insert(mediaProcessHistory);
        // 删除处理成功的文件信息
        this.baseMapper.deleteById(mediaProcess);
        return true;
    }
}
